<?php

/**
* 新版本,每个咨询类型. 单独定价
*/
namespace app\v3\controller;
use think\Db;
use think\Controller;
use app\service\AliSmsService;
use think\facade\Env;
use app\service\WxService;
use think\facade\Log;
# 接入支付
use Yurun\PaySDK\Weixin\Params\PublicParams;
use Yurun\PaySDK\Weixin\SDK;
use Yurun\PaySDK\Weixin\JSAPI\Params\Pay\Request;
use Yurun\PaySDK\Weixin\JSAPI\Params\JSParams\Request as jsRequest;
use think\Exception;

class Index
{
    /**
     * 获取用户套餐
     */
    public function getUserCombo()
    {
        $res = ['code' => 1, 'data' => [], 'msg' => ''];
        
        try {
            $reqParams = input('');
            # 入单请求参数
            if (empty($reqParams['user_phone']) || !checkPhone($reqParams['user_phone'])) {
                $res['msg'] = '用户唯一凭证格式不正确!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }
            $mField = 'once_nums,month_day,works_once_nums,works_month_nums,emotion_once_nums,emotion_month_nums,pr_once_nums,pr_month_nums,group_once_nums,group_month_nums';
            $userComboInfo = Db::name('member')->field($mField)->where(['phone' => $reqParams['user_phone']])->find();
            
            # 获取 单次/月卡的费用
            $comboInfo = Db::name('combos')->field('combo_type,fee')->select();
            
            $res['code'] = 0;
            # 项目预测
            $res['data']['once_nums'] = isset($userComboInfo['once_nums']) ? $userComboInfo['once_nums'] : 0;
            $res['data']['month_day'] = isset($userComboInfo['month_day']) ? $userComboInfo['month_day'] : 0;
            # 项目预测, 单次和月卡费用
            $res['data']['oncepay'] = isset($comboInfo[0]['fee']) ? $comboInfo[0]['fee'] : 0;
            $res['data']['monpay'] = isset($comboInfo[1]['fee']) ? $comboInfo[1]['fee'] : 0;
            
            # 工作事业
            $res['data']['works_once_nums'] = isset($userComboInfo['works_once_nums']) ? $userComboInfo['works_once_nums'] : 0;
            $res['data']['works_month_nums'] = isset($userComboInfo['works_month_nums']) ? $userComboInfo['works_month_nums'] : 0;
            # 工作事业, 单次和月卡费用
            $res['data']['works_oncepay'] = isset($comboInfo[2]['fee']) ? $comboInfo[2]['fee'] : 0;
            $res['data']['works_monpay'] = isset($comboInfo[3]['fee']) ? $comboInfo[3]['fee'] : 0;
            
            # 情感咨询
            $res['data']['emotion_once_nums'] = isset($userComboInfo['emotion_once_nums']) ? $userComboInfo['emotion_once_nums'] : 0;
            $res['data']['emotion_month_nums'] = isset($userComboInfo['emotion_month_nums']) ? $userComboInfo['emotion_month_nums'] : 0;
            # 情感咨询, 单次和月卡费用
            $res['data']['emotion_oncepay'] = isset($comboInfo[4]['fee']) ? $comboInfo[4]['fee'] : 0;
            $res['data']['emotion_monpay'] = isset($comboInfo[5]['fee']) ? $comboInfo[5]['fee'] : 0; 
            
            # 公关审核
            $res['data']['pr_once_nums'] = isset($userComboInfo['pr_once_nums']) ? $userComboInfo['pr_once_nums'] : 0;
            $res['data']['pr_month_nums'] = isset($userComboInfo['pr_month_nums']) ? $userComboInfo['pr_month_nums'] : 0;
            # 公关审核, 单次和月卡费用
            $res['data']['pr_oncepay'] = isset($comboInfo[6]['fee']) ? $comboInfo[6]['fee'] : 0;
            $res['data']['pr_monpay'] = isset($comboInfo[7]['fee']) ? $comboInfo[7]['fee'] : 0;

            # 团体套餐
            $res['data']['group_once_nums'] = isset($userComboInfo['group_once_nums']) ? $userComboInfo['group_once_nums'] : 0;
            $res['data']['group_month_nums'] = isset($userComboInfo['group_month_nums']) ? $userComboInfo['group_month_nums'] : 0;
            # 团体套餐, 单次和月卡费用
            $res['data']['group_oncepay'] = isset($comboInfo[8]['fee']) ? $comboInfo[8]['fee'] : 0;
            $res['data']['group_monpay'] = isset($comboInfo[9]['fee']) ? $comboInfo[9]['fee'] : 0;  
            
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));
        }  catch (Exception $e) {
            $res['msg'] = $e->getMessage();
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));            
        }        
    }
    /**
     * 最新版本AI预测 
     */
    public function v3latestPersonPredict()
    {
        $params = input('');
        # 需要展示时间节点预测. personal_consult_show
        $personConsultShow = config('personal_consult_show');
        $personConf = config('personal_consult_tmpl');
        # 大类型
        $type  = isset($params['types']) ? $params['types'] : 0;
        # 大栏目下的 子菜单
        $itypes  = isset($params['itypes']) ? $params['itypes'] : 0;
        # 用户填写的信息
        $personMsg = isset($params['person_fix_msg']) ? $params['person_fix_msg'] : '';
        # 职位. 该字段 仅在工作事业栏目有
        $jobTitle = isset($params['person_job']) ? $params['person_job'] : '';
        # 该参数指的是, 当前咨询对应 的 结果ID
        $id = isset($params['id']) ? $params['id'] : 0;
        $res = ['code' => 0, 'data' => [], 'msg' => ''];
        
        if (empty($type) && empty($id))   exit(json_encode($res, JSON_UNESCAPED_UNICODE));
        
        # 没有ID的情况,根据type取出随机数
        if (empty($id)) {
            # 查询当前用户, 在当前类型下,是否有可用套餐
            
            # 用户无可用套餐, 不允许咨询
            $memField = 'id,once_nums,month_day,works_once_nums,works_month_nums,emotion_once_nums,emotion_month_nums,pr_once_nums,pr_month_nums';
            $userComboInfo = Db::name('member')->field($memField)->where(['phone' => $params['user_phone']])->find();  
            # 根据每个咨询
            switch ($type) {
                case 1:
                    if ($userComboInfo['once_nums'] <=0 && $userComboInfo['month_day'] <= 0) {
                        $res['code'] = 1;
                        $res['msg'] = '项目咨询-无可用咨询套餐';
                        exit(json_encode($res, JSON_UNESCAPED_UNICODE));
                        exit;                
                    }                    
                    break;
                case 2:
                    if ($userComboInfo['works_once_nums'] <=0 && $userComboInfo['works_month_nums'] <= 0) {
                        $res['code'] = 1;
                        $res['msg'] = '工作事业-无可用咨询套餐';
                        exit(json_encode($res, JSON_UNESCAPED_UNICODE));
                        exit;                
                    }                    
                    break;
                case 3:
                    if ($userComboInfo['emotion_once_nums'] <=0 && $userComboInfo['emotion_month_nums'] <= 0) {
                        $res['code'] = 1;
                        $res['msg'] = '情感咨询-无可用咨询套餐';
                        exit(json_encode($res, JSON_UNESCAPED_UNICODE));
                        exit;                
                    }                    
                    break;
                case 4:
                    if ($userComboInfo['pr_once_nums'] <=0 && $userComboInfo['pr_month_nums'] <= 0) {
                        $res['code'] = 1;
                        $res['msg'] = '公关审核-无可用咨询套餐';
                        exit(json_encode($res, JSON_UNESCAPED_UNICODE));
                        exit;                
                    }                    
                    break;                    
            }

            # 根据 大小菜单, 确认用户提问信息
            $personTpl = isset($personConf[$type]['items'][$itypes]) ? $personConf[$type]['items'][$itypes] : '';
            
            # 如果是 工作事业选择
            if ($itypes == 4) $personTpl = sprintf($personTpl, $jobTitle, $personMsg);
            # 非工作事业选择
            if ($itypes != 4) $personTpl = sprintf($personTpl, $personMsg);   
            
            # 类型不能为空
            if (empty($type)) {
                $res['code'] = 0;
                $res['msg'] = '咨询类型不能为空!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
                exit;
            }
            
            # 新增字段
            $phone = (isset($params['user_phone']) && !empty($params['user_phone'])) ? $params['user_phone'] : '';
            # 手机号不能为空
            if (empty($phone)) {
                $res['code'] = 0;
                $res['msg'] = '手机号为必填参数';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
                exit;
            }            
            # 随机取一个. 如果是: 项目预测: 1 + 公关审核
            if ($type == 1 || $type == 4)   $typeIds = Db::name('docs_latest')->field('id')->select();
            # 随机取一个. 如果是: 工作事业选择: 2
            if ($type == 2)   $typeIds = Db::name('docs_works')->field('id')->select();
            # 随机取一个. 如果是: 情感咨询选择: 3
            if ($type == 3)   $typeIds = Db::name('docs_emotion')->field('id')->select();
            
            $typeIdsArr = array_column($typeIds, 'id');
            # 从数组里面随机取出一个
            $key    = array_rand($typeIdsArr);
            $id     = $typeIdsArr[$key];

            $info = [];
            if ($type == 1 || $type == 4)   $info   = Db::name('docs_latest')->field('*')->where(['id' => $id])->find();
            # 随机取一个. 如果是: 工作事业选择: 2
            if ($type == 2)   $info   = Db::name('docs_works')->field('*')->where(['id' => $id])->find();
            # 随机取一个. 如果是: 情感咨询选择: 3
            if ($type == 3)   $info   = Db::name('docs_emotion')->field('*')->where(['id' => $id])->find();
            # 将当前用户的, 提交记录 存储
            $isShowTimes = in_array($itypes, $personConsultShow['is_show_time_ids']) ? 1 : 0;

            $insertData = [
                'result_id' => $id,
                'user_phone' => $phone,
                'person_menu_id' => $type,
                'person_menu_item_id' => $itypes,
                'person_fix_msg' => $personMsg,
                'person_job' => $jobTitle,
                'is_show_time_res' => $isShowTimes,
                'add_time'=> date('Y-m-d H:i:s', time())
            ];
            $historyId = Db::name('new_user_question_history')->insertGetId($insertData);
            
            $info['is_show_time_res'] = $isShowTimes;
            $info['person_tpl'] = $personTpl;
            $info['history_id'] = $historyId;
            $info['types'] = $type;
            $info['itypes'] = $itypes;
            # 循环数据里面的 每个 value.替换 \n为 <br/>
            if (!empty($info)) {
                foreach ($info as $kItem => $kVal) {
                    $info[$kItem] = str_replace('\n', '<br/>', $info[$kItem]);
                }
            }
            $res['data'] = $info;
            # TODO: 传入combo_type
            $comboType = isset($params['comboType']) ? $params['comboType'] : 100;
            # 无月卡套餐, 扣减次数.有月卡继续执行
            switch ($type) {
                case 1:
                    if ($userComboInfo['month_day'] <= 0) {
                        # 用户表扣减次数
                        Db::name('member')->where(['phone' => $params['user_phone']])->setDec('once_nums');        
                    }
                    break;
                case 2:
                    if ($userComboInfo['works_month_nums'] <= 0) {
                        # 用户表扣减次数
                        Db::name('member')->where(['phone' => $params['user_phone']])->setDec('works_once_nums');        
                    }                   
                    break;
                case 3:
                    if ($userComboInfo['emotion_month_nums'] <= 0) {
                        # 用户表扣减次数
                        Db::name('member')->where(['phone' => $params['user_phone']])->setDec('emotion_once_nums');
                    }                   
                    break;
                case 4:
                    if ($userComboInfo['pr_month_nums'] <= 0) {
                        # 用户表扣减次数
                        Db::name('member')->where(['phone' => $params['user_phone']])->setDec('pr_once_nums');        
                    }                   
                    break;                    
            }            
            # Log::info('提问记录::'. json_encode($insertData, JSON_UNESCAPED_UNICODE));
            
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));
        } else {
            # 个人 咨询,历史记录
            $historyId = isset($params['history_id']) ? $params['history_id'] : 0;
            
            if (empty($historyId)) {
                $res['msg'] = '咨询记录ID不能为空!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }   
            # 根据 $historyId, 得出它咨询所 得到的答案ID
            $historyInfo = Db::name('new_user_question_history')->field('person_fix_msg,person_job,result_id')->where(['id' => $historyId])->find();
            
            $resultId = $id;
            // $resultId = isset($historyInfo['result_id']) ? $historyInfo['result_id'] : 0;

            if (empty($resultId)) {
                $res['msg'] = '未查询到咨询结果!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));                
            }
            # 根据 大小菜单, 确认用户提问信息
            $personTpl = isset($personConf[$type]['items'][$itypes]) ? $personConf[$type]['items'][$itypes] : '';
            # 如果是 工作事业选择
            if ($itypes == 4) $personTpl = sprintf($personTpl, $historyInfo['person_fix_msg'], $historyInfo['person_job']);
            # 非工作事业选择
            if ($itypes != 4) $personTpl = sprintf($personTpl, $historyInfo['person_fix_msg']);
            
            # 随机取一个. 如果是: 项目预测: 1 + 公关审核
            if ($type == 1 || $type == 4) $info = Db::name('docs_latest')->field('*')->where(['id' => $resultId])->find();
            # 随机取一个. 如果是: 工作事业选择: 2
            if ($type == 2)   $info = Db::name('docs_works')->field('*')->where(['id' => $resultId])->find();
            # 随机取一个. 如果是: 情感咨询选择: 3
            if ($type == 3)   $info = Db::name('docs_emotion')->field('*')->where(['id' => $resultId])->find();
                   
            $info['is_show_time_res'] = 1;
            $info['person_tpl'] = $personTpl;
            $res['code'] = 0;
            # 循环数据里面的 每个 value.替换 \n为 <br/>
            if (!empty($info)) {
                foreach ($info as $kItem => $kVal) {
                    $info[$kItem] = str_replace('\n', '<br/>', $info[$kItem]);
                }
            }
            
            # if (isset($info['twenty_years_predict']))   $info['twenty_years_predict'] = str_replace('\n', '<br/>', $info['twenty_years_predict']);
            $res['data'] = $info;

            exit(json_encode($res, JSON_UNESCAPED_UNICODE));            
        }
        exit;        
    }    
    /**
     * 获取当前用户 是否可以使用 咨询 
     */
    public function v2getUserIsUse()
    {
        $params = input('');
        $res = ['code' => 1, 'data' => [], 'msg' => ''];
        
        if (empty($params['user_phone'])) {
            $res['msg'] = '用户唯一凭证为必传标识';
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));
        }
        # 1-项目咨询,2-工作事业,3-情感咨询,4-公关审核
        $allowTypes = [1, 2, 3, 4];
        # 套餐类型, 为必传参数
        if (empty($params['type']) || !in_array($params['type'], $allowTypes)) {
            $res['msg'] = '套餐类型为必传参数';
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));
        }
        $type = $params['type'];
        # 获取单次付费标准
        $comboVal = [0];
        if ($type == 1) $comboVal = Db::name('combos')->where(['combo_type' => 1])->column('fee');
        if ($type == 2) $comboVal = Db::name('combos')->where(['combo_type' => 3])->column('fee');
        if ($type == 3) $comboVal = Db::name('combos')->where(['combo_type' => 5])->column('fee');
        if ($type == 4) $comboVal = Db::name('combos')->where(['combo_type' => 7])->column('fee');
        
        $res['data']['zifei'] = isset($comboVal[0]) ? $comboVal[0] : 0;
        $res['data']['times'] = 1;
        
        # 获取用户是有当前类型的套餐
        $mField = 'once_nums,month_day,works_once_nums,works_month_nums,emotion_once_nums,emotion_month_nums,pr_once_nums,pr_month_nums';
        $memInfo = Db::name('member')->field($mField)->where(['phone' => $params['user_phone']])->find();
        
        if (empty($memInfo))
        {
            $res['code'] = 0;
            $res['data']['is_used'] = 0;
            
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));            
        }
        # 根据用户的 咨询类型. 获取是否有咨询次数
        if ($type == 1 && $memInfo['once_nums'] < 1 && $memInfo['month_day'] < 1) {
            $res['code'] = 0;
            $res['data']['is_used'] = 0;
            
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));               
        }
        # 查询工作事业
        if ($type == 2 && $memInfo['works_once_nums'] < 1 && $memInfo['works_month_nums'] < 1) {
            $res['code'] = 0;
            $res['data']['is_used'] = 0;
            
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));               
        }
        # 查询情感咨询
        if ($type == 3 && $memInfo['emotion_once_nums'] < 1 && $memInfo['emotion_month_nums'] < 1) {
            $res['code'] = 0;
            $res['data']['is_used'] = 0;
            
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));               
        }
        # 查询公关审核
        if ($type == 4 && $memInfo['pr_once_nums'] < 1 && $memInfo['pr_month_nums'] < 1) {
            $res['code'] = 0;
            $res['data']['is_used'] = 0;
            
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));               
        }

        # 只有有一种场景, 都可以满足
        $res['code'] = 0;
        $res['data']['is_used'] = 1;
        exit(json_encode($res, JSON_UNESCAPED_UNICODE));        
    }
    /**
     * v3: 入单接口--得带上当前, 咨询类型
     */
    public function intoOrder()
    {
        $res = ['code' => 1, 'data' => [], 'msg' => ''];
        try {
            $reqParams = input('');

            # 入单请求参数
            if (empty($reqParams['user_phone'])) {
                $res['msg'] = '用户唯一凭证为必传标识';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }
            
            $comboType = isset($reqParams['combo_type']) ? $reqParams['combo_type'] : -1;
            $types = isset($reqParams['type']) ? $reqParams['type'] : -1;
            
            # 咨询类型. 总共分4个类型. TODO: 可使用数据库 管理
            $allowTypes = [1, 2, 3, 4, 5];
            if (!in_array($types, $allowTypes)) {
                $res['msg'] = '咨询类型为必传!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }            
            # 套餐类型: 0-单次咨询,1-包月套餐
            $comboTypeArr = [0, 1];
            
            if (!in_array($comboType, $comboTypeArr)) {
                $res['msg'] = '套餐标识错误';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));          
            }
            # 获取当前类型下, 每个套餐的数据表信息
            $miniComboConf = config('app.mini_combo');
            $tempTblComboType = $miniComboConf[$types][$comboType];
            
            if (empty($tempTblComboType)) {
                $res['msg'] = '套餐匹配失败!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));                 
            }
            # 获取用户openid
            $mField = 'openid,once_nums,month_day,works_once_nums,works_month_nums,emotion_once_nums,emotion_month_nums,pr_once_nums,pr_month_nums,group_once_nums,group_month_nums';
            $memInfo = Db::name('member')->field($mField)->where(['phone' => $reqParams['user_phone']])->find();
            $openid = isset($memInfo['openid']) ? $memInfo['openid'] : '';
            
            if (empty($openid)) {
                $res['msg'] = '微信用户标识错误';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));                 
            }
            # 当前用户有 包月套餐, 不允许购买单次套餐(月卡未过期之前不允许购买)
            # 根据用户的 咨询类型. 获取是否有咨询次数
            if ($types == 1 && $memInfo['month_day'] >= 1) {
                $res['msg'] = '当前已有包月套餐!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }
            if ($types == 2 && $memInfo['works_month_nums'] >= 1) {
                $res['msg'] = '当前已有包月套餐!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }
            if ($types == 3 && $memInfo['emotion_month_nums'] >= 1) {
                $res['msg'] = '当前已有包月套餐!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }
            if ($types == 4 && $memInfo['pr_month_nums'] >= 1) {
                $res['msg'] = '当前已有包月套餐!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }
            if ($types == 5 && $memInfo['group_month_nums'] >= 1) {
                $res['msg'] = '当前已有包月套餐!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }
            # 根据套餐 类型获取支付金额
            $comboVal = Db::name('combos')->where(['combo_type' => $tempTblComboType])->column('fee');
            # TODO
            if (!isset($comboVal[0]) || $comboVal[0] <= 0) {
                $res['msg'] = '套餐资费配置错误';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));              
            }

            $amount = $comboVal[0];       
            $content = 'AI分析-咨询服务费';
            $paymentSn = 'AI_' . date('YmdHis').'_'.mt_rand(10000, 99999);
    
            $params = new PublicParams;
            # 微信支付配置
            $wechatPayConf = config('app.wechat_pay');
            # 支付平台分配给开发者的应用ID 
            $params->appID = $wechatPayConf['mini_appid'];
            # 微信支付分配的商户号
            $params->mch_id = $wechatPayConf['mch_id'];
            # 商户API 密钥 
            $params->key = $wechatPayConf['mch_secret'];
            # SDK实例化，传入公共配置
            $pay = new SDK($params);
            # 支付接口（生成支付订单）
            $request = new Request;
            $request->body = $content;
            $request->out_trade_no = $paymentSn;
            # 支付金额 换算成: xx分
            $request->total_fee = $amount*100;
            # $request->spbill_create_ip = '127.0.0.1';
            $request->notify_url = env('app.domain', 'https://docs.wmta.cn/').'v3/index/recvNotify';
            $request->openid = $openid;
    
            # 调用接口
            $result = $pay->execute($request);
            if($pay->checkResult())
            {
                $request = new jsRequest;
                $request->prepay_id = $result['prepay_id'];
                $jsapiParams = $pay->execute($request);
                # 增加本次发起支付的订单编号
                $insertData = [
                    'user_phone' => $reqParams['user_phone'],
                    'order_sn' => $paymentSn,
                    // 'combo_type' => $comboType,
                    'combo_type' => $tempTblComboType,
                    'order_amount' => $amount,
                    'add_time' => date('Y-m-d H:i:s', time())
                ];
                $insertRes = Db::name('zifei_order')->insert($insertData);
                
                if (!$insertRes)    throw new Exception('支付系统繁忙,请稍后再试!');
                
                $res['code'] = 0;
                $res['data'] = $jsapiParams;
                
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));           
            } else {
                $res['msg'] = '微信支付,请求失败!';
                exit(json_encode($res, JSON_UNESCAPED_UNICODE));
            }            
        } catch (Exception $e) {
            $res['msg'] = $e->getMessage();
            exit(json_encode($res, JSON_UNESCAPED_UNICODE));            
        }
    }
    /**
     * 回调接口
     */
    public function recvNotify()
    {
        $reqParams = file_get_contents('php://input');
        # Log::info('微信支付回调::'. $reqParams);
        # 解析通知的xml
        $xmlArr = parserXMLToArray($reqParams, 'array');
        # 本站订单号
        $orderSn = isset($xmlArr['out_trade_no']) ? $xmlArr['out_trade_no'] : '';
        $resultCode = isset($xmlArr['result_code']) ? $xmlArr['result_code'] : '';
        $transactionId = isset($xmlArr['transaction_id']) ? $xmlArr['transaction_id'] : '';

        if ($resultCode == 'SUCCESS') {
            # 更新订单信息
            $updateData = [
                'pay_time' => date('Y-m-d H:i:s', time()),
                'pay_status' => 1,
                'transaction_sn' => $transactionId
            ];
            
            # Log::info('订单更新结果::', ['update_res' => true]);
            # 增加用户的套餐次数
            $orderInfo = Db::name('zifei_order')->field('user_phone,combo_type,pay_status')->where(['order_sn' => $orderSn, 'pay_status' => 0])->find();
            
            if (!empty($orderInfo) && isset($orderInfo['combo_type']) && isset($orderInfo['user_phone'])) {
                # 套餐增加的天数, 索引=>套餐类型, 值=>体验的 天数.0下标=>代表次数
                $comboDayArr = [1, 30];
                # 根据咨询类型&&购买的套餐类型, 得出应该增加到member的哪个字段
                $comboMemFields = config('app.mini_combo_field');
                # 根据套餐类型. 单数--次卡,双数--月卡
                $incNum = ($orderInfo['combo_type']%2 == 0) ? $comboDayArr[1] : $comboDayArr[0];
                
                Db::startTrans();
                $incMemRes = Db::name('member')->where(['phone' => $orderInfo['user_phone']])->setInc($comboMemFields[$orderInfo['combo_type']], $incNum);
                # 更新订单状态
                $orderRes = Db::name('zifei_order')->where(['order_sn' => $orderSn, 'pay_status' => 0])->update($updateData);
                
                if (!$incMemRes || !$orderRes) {
                    Db::rollback();
                    Log::info('回调事务执行失败!::', ['incMemRes' => $incMemRes, 'orderRes' => $orderRes]);
                }
                Db::commit();
            } else {
                Log::info('未查询到订单详情::', ['order_sn' => $orderSn]);
            }
        }

        echo 'success';
        exit;
    }    
}